package com.eva.databuilder.builders.cms;

import com.eva.cms.biz.article.CmsArticleBiz;
import com.eva.cms.biz.article.dto.QueryCmsArticleDTO;
import com.eva.cms.biz.article.vo.CmsArticleProfileVO;
import com.eva.databuilder.builders.AbstractTemplateDataBuilder;
import com.eva.databuilder.bo.TemplateDataParameters;
import com.eva.constants.CmsArticleStatusEnum;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 构建文章分页数据
 */
@Component
public class ArticlePageBuilder extends AbstractTemplateDataBuilder<PageData<CmsArticleProfileVO>> {

    private static final String PARAMETER_CATEGORY_UID = "categoryUid";

    private static final String PARAMETER_KEYWORD = "keyword";

    private static final String PARAMETER_PAGE = "page";

    private static final String PARAMETER_CAPACITY = "capacity";

    @Resource
    private CmsArticleBiz cmsArticleBiz;

    @Override
    public PageData<CmsArticleProfileVO> build(TemplateDataParameters parameters) {
        PageWrap<QueryCmsArticleDTO> pageWrap = new PageWrap<>();
        pageWrap.setPage(parameters.getInteger(PARAMETER_PAGE, 1));
        pageWrap.setCapacity(parameters.getInteger(PARAMETER_CAPACITY, 10));
        // 构造查询条件
        QueryCmsArticleDTO dto = new QueryCmsArticleDTO();
        // - 指定栏目
        dto.setCategoryUid(parameters.getString(PARAMETER_CATEGORY_UID, null));
        // - 仅查询在线的文章
        dto.setStatus(CmsArticleStatusEnum.ONLINE.name());
        // - 指定关键字
        dto.setKeyword(parameters.getString(PARAMETER_KEYWORD, ""));
        pageWrap.setModel(dto);
        return cmsArticleBiz.findProfilePage(pageWrap);
    }
}
